import { store } from "@/store/index.js"

/**
 * 获取时间格式
 * @param curFormat     需要的格式
 * @param dateTime      时间戳  默认为空
 * @returns {string}    格式化的时间
 */
const getDate = (curFormat = 'y-m-d h:i:s', dateTime = null) => {

	curFormat = curFormat.toLowerCase();

	let date = dateTime ? new Date(dateTime) : new Date();
	let y = date.getFullYear();
	let m = date.getMonth() + 1;
	let d = date.getDate();
	let h = date.getHours();
	let i = date.getMinutes();
	let s = date.getSeconds();

	handleData('y', y);
	handleData('m', m);
	handleData('d', d);
	handleData('h', h);
	handleData('i', i);
	handleData('s', s);

	function handleData(str, data) {
		curFormat = curFormat.replace(str, data >= 10 ? data : ('0' + data))
	}

	return curFormat;
}

/**
 * 获取本周开始日期
 */
const getWeekStart = () => {
	let date = new Date();
	let y = date.getFullYear();
	let m = date.getMonth() + 1;
	let d = date.getDate();
	let w = date.getDay();

	let weekStarTime = new Date(y, m, d - w + 1);

	let weekStarDate = getDate('y-m-d', weekStarTime);
	console.log(weekStarDate);
	return weekStarDate;
}

/**
 * 数字转中文
 * @param {*} num 
 */
const toChinesNum = (num) => {
	if (Number(num) != parseInt(num)) {
		return '';
	}
	let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
	let unit = ['', '十', '百', '千', '万']
	num = parseInt(num)
	let getWan = (temp) => {
		let strArr = temp.toString().split('').reverse()
		let newNum = ''
		let newArr = []
		strArr.forEach((item, index) => {
			newArr.unshift(item === '0' ? changeNum[item] : changeNum[item] + unit[index])
		})
		let numArr = []
		newArr.forEach((m, n) => {
			if (m !== '零') numArr.push(n)
		})
		if (newArr.length > 1) {
			newArr.forEach((m, n) => {
				if (newArr[newArr.length - 1] === '零') {
					if (n <= numArr[numArr.length - 1]) {
						newNum += m
					}
				} else {
					newNum += m
				}
			})
		} else {
			newNum = newArr[0]
		}
		return newNum
	}
	let overWan = Math.floor(num / 10000)
	let noWan = num % 10000
	if (noWan.toString().length < 4) {
		noWan = '0' + noWan
	}
	let str = overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num);
	let index = str.indexOf('一十');
	if (index == 0) {
		let new_str = str.substr(1);
		return new_str;
	}
	return str;
}

/**
 * 拼接style样式
 */
const joinStyle = (styles) => {
	let str = '';
	if (typeof styles == "object") {
		let arr = [];
		for (let i in styles) {
			arr.push(i + ':' + styles[i]);
		}
		str = arr.join(';');
	} else {
		str = styles;
	}
	return str ? (str + ';') : ''
}

/**
 * 数字拼接单位rpx
 * @param {Object} value
 */
const getUnitSize = (value) => {
	if (/^[+-]?\d*\.?\d{0,}$/.test(value)) {
		value += 'rpx';
	}
	return value;
}

import md5 from '@/utils/md5.js'

/**
 * 组合请求参数
 * @param
 * [
 * 	data		请求参数
 *  needSign	是否需要签名
 *  request_type		格式默认string  支持object
 * ]
 */
const joinRequestParam = (param) => {

	let needSign = param.needSign === false ? false : true;

	let App = getApp().globalData;

	let data = param.data || {};
	let userInfo = App.XcxData.userInfo;
	let share_uid = uni.getStorageSync('share_uid') ? uni.getStorageSync('share_uid') : App.XcxData.share_uid
	data.merchant_id = App.XcxData.merchant_id; //商户id

	data.version = 2;

	if (userInfo.uid && needSign) {
		data.uid = userInfo.uid; //用户id
	}
	if (!data.vid && userInfo && userInfo.vid) {
		data.vid = userInfo.vid;
	}
	if (param.url != '/Store/getStore_v1' && !data.store_id && store.store_id && (parseInt(store.store_id) > 0)) {
		data.store_id = store.store_id; //门店id
	}

	if (App.XcxData.openid) {
		data.openid = App.XcxData.openid; //用户openid
	}
	if (!data.appId) {
		data.appId = App.XcxData.appid;
	}

	// #ifdef MP-WEIXIN
	data.original_type = 1
	// #endif
	// #ifdef MP-ALIPAY
	data.original_type = 2
	data.isIsv = App.XcxData.appid == App.XcxData.tempAppId ? true : false;
	// #endif

	if (App.XcxData.systemInfo && App.XcxData.systemInfo.model) {
		data.equip = App.XcxData.systemInfo.model;
	}

	let key = uni.getStorageSync('signKey');

	if (key && needSign) {
		let keysSorted = Object.keys(data).sort(); //排序健名

		let signArr = {};

		for (let i = 0; i < keysSorted.length; i++) {
			signArr[keysSorted[i]] = data[keysSorted[i]];
		}

		data = signArr;
	}

	let signData = {
		...data
	};

	let strData = App.modifyPostParam(data);

	let outSignFields = ['remark', 'apply_reason', 'evaluate_json'];
	if (key && needSign) {
		//剔除不需要参与签名的字段
		for (let i in outSignFields) {
			if (signData[outSignFields[i]]) {
				delete signData[outSignFields[i]];
			}
		}

		let str = App.modifyPostParam(signData);
		str += '&key=' + key;
		
		App.logDebug && console.log('sign str', str)
		
		str = decodeURIComponent(str);

		let sign = md5.md5(str);
		
		strData += '&sign=' + sign;
		data.sign = sign;
	}
	if (!param.request_type || param.request_type == 'string') {
		return strData;
	} else {
		return data
	}
}

export default {
	getDate,
	toChinesNum,
	joinStyle,
	getUnitSize,
	joinRequestParam,
}
